
using QdbUnit;
using QdbUnit.Extensions.Fixtures;
using QdbUnit.Extensions.Util;
using Qdb;
using Qdb.Config;
using Qdb.Ext;
using Qdb.Foundation.IO;
using Qdb.Internal;
using Qdb.Tests.Common.Exceptions;
using Qdb.Tests.Util;

namespace Qdb.Tests.Common.Exceptions
{
	/// <exclude></exclude>
	public class OldFormatExceptionTestCase : ITestCase, IOptOutNoFileSystemData
	{
		public static void Main(string[] args)
		{
			new ConsoleTestRunner(typeof(OldFormatExceptionTestCase)).Run();
		}

		// It is also regression test for COR-634.
		/// <exception cref="System.Exception"></exception>
		public virtual void Test()
		{
			if (WorkspaceServices.WorkspaceRoot == null)
			{
				Sharpen.Runtime.Err.WriteLine("Build environment not available. Skipping test case..."
					);
				return;
			}
			if (!System.IO.File.Exists(SourceFile()))
			{
				Sharpen.Runtime.Err.WriteLine("Test source file " + SourceFile() + " not available. Skipping test case..."
					);
				return;
			}
			string oldDatabaseFilePath = OldDatabaseFilePath();
			Assert.Expect(typeof(OldFormatException), new _ICodeBlock_42(this, oldDatabaseFilePath
				));
			IObjectContainer container = null;
			try
			{
				container = QdbFactory.OpenFile(NewConfiguration(true), oldDatabaseFilePath);
			}
			finally
			{
				if (container != null)
				{
					container.Close();
				}
			}
		}

		private sealed class _ICodeBlock_42 : ICodeBlock
		{
			public _ICodeBlock_42(OldFormatExceptionTestCase _enclosing, string oldDatabaseFilePath
				)
			{
				this._enclosing = _enclosing;
				this.oldDatabaseFilePath = oldDatabaseFilePath;
			}

			/// <exception cref="System.Exception"></exception>
			public void Run()
			{
				QdbFactory.OpenFile(this._enclosing.NewConfiguration(false), oldDatabaseFilePath
					);
			}

			private readonly OldFormatExceptionTestCase _enclosing;

			private readonly string oldDatabaseFilePath;
		}

		private IConfiguration NewConfiguration(bool allowVersionUpdates)
		{
			IConfiguration config = QdbFactory.NewConfiguration();
			config.ReflectWith(Platform4.ReflectorForType(typeof(OldFormatExceptionTestCase))
				);
			config.AllowVersionUpdates(allowVersionUpdates);
			return config;
		}

		/// <exception cref="System.IO.IOException"></exception>
		protected virtual string OldDatabaseFilePath()
		{
			string oldFile = IOServices.BuildTempPath("old_db.qdb");
			File4.Copy(SourceFile(), oldFile);
			return oldFile;
		}

		private string SourceFile()
		{
			return WorkspaceServices.WorkspaceTestFilePath("qdbVersions/qdb_3.0.3");
		}
	}
}
